%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef YYSTYPE
#define YYSTYPE char*//需要返回的是一个后缀表达式，是一个字符串
#endif

char numStr[50];
char idStr[50];
int yylex();
extern int yyparse();
FILE* yyin;
void yyerror(const char*s);
%}

%token ADD
%token SUB
%token MUL
%token DIV
%token UMINUS
%token LEFT_PAREN
%token RIGHT_PAREN
%token NUMBER
%token ID

%left ADD SUB
%left MUL DIV
%right UMINUS

%%

lines   :   lines expr ';' {printf("%s\n", $2);}
        |   lines ';'
        |   
        ;

expr    :   expr ADD expr           {
                                    $$ = (char*) malloc (50*sizeof(char));
                                    strcpy($$, $1);
                                    strcat($$, $3);
                                    strcat($$, "+ ");
                                    }
        |   expr SUB expr           {
                                    $$ = (char*) malloc (50*sizeof(char));
                                    strcpy($$, $1);
                                    strcat($$, $3);
                                    strcat($$, "- ");
                                    }
        |   expr MUL expr           {
                                    $$ = (char*) malloc (50*sizeof(char));
                                    strcpy($$, $1);
                                    strcat($$, $3);
                                    strcat($$, "* ");
                                    }
        |   expr DIV expr           {
                                    $$ = (char*) malloc (50*sizeof(char));
                                    strcpy($$, $1);
                                    strcat($$, $3);
                                    strcat($$, "/ ");
                                    }
        |   LEFT_PAREN expr RIGHT_PAREN  {
                                    $$ = (char*) malloc (50*sizeof(char));
                                    strcpy($$, $2);
                                    }
        |   SUB expr %prec UMINUS   {
                                    $$ = (char*) malloc (50*sizeof(char));
                                    strcpy($$, "-");
                                    strcat($$, $2);
                                    
                                    }
        |   NUMBER                  {
                                    $$ = (char*) malloc (50*sizeof(char));
                                    strcpy($$, $1);
                                    strcat($$, " ");
                                    }
        |   ID                      {
                                    $$ = (char*) malloc (50*sizeof(char));
                                    strcpy($$, $1);
                                    strcat($$, " ");
                                    }
        ;

%%

int yylex()
{
    int t;
    while(1){
        t = getchar();
        if(t == ' ' || t =='\t' || t == '\n');
        else if((t >= '0' && t <= '9')){
            int ti=0;
            while((t >= '0' && t <= '9')){
                numStr[ti]=t;
                t=getchar();
                ti++;
            }
            numStr[ti]='\0';
            yylval=numStr;
            ungetc(t, stdin);
            return NUMBER;
        }
         else if(( t >= 'a' && t <= 'z' ) || ( t >= 'A' && t<= 'Z' ) || ( t == '_' )){
            int i = 0;
            while(( t >= 'a' && t <= 'z' ) || ( t >= 'A' && t<= 'Z' ) || ( t == '_' ) || ( t >= '0' && t <= '9' )){
                idStr[i] = t;
                t = getchar();
                i++;
            }
            idStr[i] = '\0';
            yylval = idStr;
            ungetc(t, stdin);
            return ID;
        }
        else{
            switch(t){
    case '+':
        return ADD;
        break;
    case '-':
        return SUB;
        break;
    case '*':
        return MUL;
        break;
    case '/':
        return DIV;
        break;
    case '(':
        return LEFT_PAREN;
        break;
    case ')':
        return RIGHT_PAREN;
    default:
        return t;
        break;
   }
        }
    }
}

int main()
{
    yyin = stdin;
    do {
        yyparse();
    } while(!feof(yyin));
    return 0;
}

void yyerror(const char* s)
{
    fprintf(stderr, "Parse error:%s\n", s);
    exit(1);
}